package com.banciyuan.bcywebview.biz.zanlist;


import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.factory.TimelineFactory;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.biz.groupdetail.NewTeamDetailActivity;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.TimelineCardType;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridDailyNoPicHolder;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridGroupHolder;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridGroupTeamHolder;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridLarticleHolder;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridPicHolder;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridVideoHolder;
import com.banciyuan.bcywebview.biz.main.mainpage.timeline.viewholder.GridWriterHolder;
import com.banciyuan.bcywebview.utils.intent.gotoUtil;
import com.banciyuan.bcywebview.utils.string.StringUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.daoexample.model.Timeline;

public class RecommendListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;
    private List<Timeline> mTimelines = new ArrayList<>();
    private LayoutInflater mLayoutInflater;

    public RecommendListAdapter(Context context, List<Timeline> timelines) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mTimelines = timelines;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            return new ViewZanEmpty(mLayoutInflater.inflate(R.layout.zanlist_empty_view, parent, false));
        } else {
            switch (viewType) {
                case TimelineCardType.GRID_PIC:
                default:
                    return new GridPicHolder(View.inflate(mContext, R.layout.timeline_grid_pic, null));
                case TimelineCardType.GRID_DAILY_NOPIC:
                    return new GridDailyNoPicHolder(View.inflate(mContext, R.layout.timeline_grid_daily_without_pic, null));
                case TimelineCardType.GRID_LARTICLE:
                    return new GridLarticleHolder(View.inflate(mContext, R.layout.timeline_grid_larticle, null));
                case TimelineCardType.GRID_VIDEO:
                    return new GridVideoHolder(View.inflate(mContext, R.layout.timeline_grid_video, null));
                case TimelineCardType.GRID_GROUP:
                    return new GridGroupTeamHolder(View.inflate(mContext, R.layout.timeline_grid_group_team, null));
                case TimelineCardType.GRID_GROUP_POST:
                    return new GridGroupHolder(View.inflate(mContext, R.layout.timeline_grid_group, null));
                case TimelineCardType.GRID_WRITER:
                    return new GridWriterHolder(View.inflate(mContext, R.layout.timeline_grid_writer, null));
            }
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (position >= 1) {
            int index = position - 1;
            Timeline mItem = mTimelines.get(index);
            OnItemClick onclick = new OnItemClick(mItem);
            switch (getItemViewType(position)) {
                case TimelineCardType.GRID_PIC:
                default:
                    GridPicHolder gridPicHolder = (GridPicHolder) holder;
                    gridPicHolder.setGridPic(gridPicHolder, mItem, onclick);
                    break;
                case TimelineCardType.GRID_DAILY_NOPIC:
                    GridDailyNoPicHolder gridDailyNoPicHolder = (GridDailyNoPicHolder) holder;
                    gridDailyNoPicHolder.setGridDailyNoPic(gridDailyNoPicHolder, mItem, onclick);
                    break;
                case TimelineCardType.GRID_LARTICLE:
                    GridLarticleHolder gridLarticleHolder = (GridLarticleHolder) holder;
                    gridLarticleHolder.setGridarticle(gridLarticleHolder, mItem, onclick);
                    break;
                case TimelineCardType.GRID_VIDEO:
                    GridVideoHolder gridVideoHolder = (GridVideoHolder) holder;
                    gridVideoHolder.setGridPic(gridVideoHolder, mItem, onclick);
                    break;
                case TimelineCardType.GRID_GROUP:
                    GridGroupTeamHolder gridGroupTeamHolder = (GridGroupTeamHolder) holder;
                    gridGroupTeamHolder.setGridgroupTeam(gridGroupTeamHolder, mItem, onclick);
                    break;
                case TimelineCardType.GRID_GROUP_POST:
                    GridGroupHolder gridGroupHolder = (GridGroupHolder) holder;
                    gridGroupHolder.setGridgroup(gridGroupHolder, mItem, onclick);
                    break;
                case TimelineCardType.GRID_WRITER:
                    GridWriterHolder gridWriterHolder = (GridWriterHolder) holder;
                    gridWriterHolder.setGridWriter(gridWriterHolder, mItem, onclick);
                    break;
            }
        } else if (position == 0 && getItemViewType(position) == 0) {
            ViewZanEmpty viewZanEmpty = (ViewZanEmpty) holder;
            if (getItemCount() == 1) {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, RecyclerView.LayoutParams.MATCH_PARENT);
                viewZanEmpty.itemView.setLayoutParams(layoutParams);
                viewZanEmpty.itemView.setVisibility(View.VISIBLE);
            } else {
                RecyclerView.LayoutParams layoutParams = new RecyclerView.LayoutParams(RecyclerView.LayoutParams.MATCH_PARENT, 1);
                viewZanEmpty.itemView.setLayoutParams(layoutParams);
                viewZanEmpty.itemView.setVisibility(View.GONE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return 0;
        } else {
            Timeline mItem = mTimelines.get(position - 1);
            switch (TimelineFactory.whichType(mItem)) {
                case TimelineFactory.TIMELINE_TYPE_ILLUST:
                case TimelineFactory.TIMELINE_TYPE_COSER:
                default:
                    return TimelineCardType.GRID_PIC;
                case TimelineFactory.TIMELINE_TYPE_DAILY:
                    if (StringUtil.notNullEqual(mItem.getType(), "larticle")) {
                        return TimelineCardType.GRID_LARTICLE;
                    } else if (StringUtil.notNullEqual(mItem.getType(), "svideo")) {
                        return TimelineCardType.GRID_VIDEO;
                    } else {
                        if (TextUtils.isEmpty(mItem.getCover())) {
                            return TimelineCardType.GRID_DAILY_NOPIC;
                        } else {
                            return TimelineCardType.GRID_PIC;
                        }
                    }
                case TimelineFactory.TIMELINE_TYPE_WRITER:
                    return TimelineCardType.GRID_WRITER;
                case TimelineFactory.TIMELINE_TYPE_GROUP_TEAM:
                    return TimelineCardType.GRID_GROUP;
                case TimelineFactory.TIMELINE_TYPE_GROUP:
                    return TimelineCardType.GRID_GROUP_POST;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mTimelines.size() + 1;
    }


    private class OnItemClick implements View.OnClickListener {
        private Timeline mItem;

        public OnItemClick(Timeline mItem) {
            this.mItem = mItem;
        }

        @Override
        public void onClick(View v) {
            int whichType = TimelineFactory.whichType(mItem.getOtype_data(), mItem.getOtype());
            switch (v.getId()) {
                case R.id.write_item_one:
                    switch (whichType) {
                        case TimelineFactory.TIMELINE_TYPE_COSER:
                            DetailDataHelper.gotoCosDetail(mContext, mItem.getRp_id(), mItem.getCp_id(), false);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_ILLUST:
                            DetailDataHelper.gotoDrawerDetail(mContext, mItem.getRp_id(), mItem.getDp_id(), false);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_DAILY:
                            DetailDataHelper.gotoDailyDetail(mContext, mItem.getUd_id(), false);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_WRITER:
                            DetailDataHelper.gotoWriterDetail(mContext, mItem.getRp_id(), mItem.getWp_id(), false);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_GROUP:
                            DetailDataHelper.gotoGroupDetail(mContext, mItem.getGid(), mItem.getPost_id(), false);
                            break;
                        case TimelineFactory.TIMELINE_TYPE_GROUP_TEAM:
                            gotoUtil.gotoAct(mContext, NewTeamDetailActivity.class, mItem.getGid());
                            break;
                    }
                    break;
            }
        }
    }

    private class ViewZanEmpty extends RecyclerView.ViewHolder {

        public ViewZanEmpty(View itemView) {
            super(itemView);
        }
    }
}
